#define _CRT_SECURE_NO_WARNINGS 2

#include"stack.h"

void StackInit(stack* st) {
	assert(st);
	st->capacity = 0;
	st->top = 0;
	st->arr = NULL;
}

void StackPush(stack* st,datatype val) {
	assert(st);
	if (st->top == st->capacity) {
		st->capacity = st->capacity == 0 ? 6 : st->capacity * 2;
		st->arr = (datatype*)realloc(st->arr, st->capacity * sizeof(datatype));
		if (!st->arr) {
			perror("st->arr realloc fail");
			return;
		}
	}
	st->arr[st->top++] = val;
}

void StackPop(stack* st) {
	assert(st);
	assert(!StackEmpty(st));
	st->top--;
}

bool StackEmpty(stack* st) {
	return st->top == 0;
}

datatype StackTop(stack* st) {
	assert(st);
	return st->arr[st->top - 1];
}

void StackDestroy(stack* st) {
	st->top = 0;
	st->capacity = 0;
	free(st->arr);
	st->arr = NULL;
}

int StackSize(stack* st) {
	return st->top;
}
